package com.samsung.controller.lottery;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.samsung.common.annotation.LoginAuth;
import com.samsung.common.util.BaseUtil;
import com.samsung.model.lottery.LotteryEvent;
import com.samsung.service.lottery.LotteryService;

@Controller
@RequestMapping("/lottery/router")
public class RouterLotteryController  extends MultiActionController{
	
	@Autowired
	private LotteryService lotteryService;
	
	/**
	 * 
	 * @Title: route 
	 * @Description: TODO(抽奖的路由) 
	 * @param @param request
	 * @param @param response
	 * @param @param lid
	 * @param @throws Exception    设定文件 
	 * @return void    返回类型 
	 * @throws 
	 * @author rui1.liang
	 */
	@LoginAuth
	@RequestMapping("/route")
	public void route(HttpServletRequest request, HttpServletResponse response, String lid)throws Exception{
		if(BaseUtil.isEmpty(lid)){
			throw new Exception("抽奖主题编号不能为空");
		}
		LotteryEvent event = lotteryService.getLotteryEvent(lid);
		int cnt = lotteryService.getRecordCount(lid);

		if(cnt <= 0){
			//抽奖未抽过该主题
			if(event.getType() == 1) {
				response.sendRedirect(request.getContextPath()+"/center/lottery/show.html?lid="+lid);
			} else if(event.getType() == 2) {
				response.sendRedirect(request.getContextPath()+"/center/lottery/lottery.html?lid="+lid);
			}
		}else{
			//抽奖抽过该主题
			if(event.getType() == 1) {
				response.sendRedirect(request.getContextPath()+"/center/lottery/result.html?lid="+lid);
			} else if(event.getType() == 2) {
				response.sendRedirect(request.getContextPath()+"/center/lottery/result.html?lid="+lid);
			}
		}
	}

}
